﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GAscheduler
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Facade.init();
        }

        private void Form1_Closing(object sender, EventArgs e)
        {
            Facade.destroy();
        }

        private void btnBrowse1_Click(object sender, EventArgs e)
        {
            CallOpenFileDialog("Courses");
        }

        private void btnBrowse2_Click(object sender, EventArgs e)
        {
            CallOpenFileDialog("Rooms");
        }

        private void btnBrowse3_Click(object sender, EventArgs e)
        {
            CallOpenFileDialog("Periods");
        }

        private void CallOpenFileDialog(string sFile)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";

            if (sFile == "Courses")
                openFileDialog1.Filter = "Excel Files (*.xls)|*.xls";
            else if (sFile == "Rooms" || sFile == "Periods")
                openFileDialog1.Filter = "CSV Files (*.csv)|*.csv";

            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    // Display the path in one of the textboxes:

                    if (sFile == "Courses")
                        tbBrowse1.Text = openFileDialog1.FileName;
                    else if (sFile == "Rooms")
                        tbBrowse2.Text = openFileDialog1.FileName;
                    else if (sFile == "Periods")
                        tbBrowse3.Text = openFileDialog1.FileName;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
        }


        private void btnLoad_Click(object sender, EventArgs e)
        {

            Facade.LoadCourses(tbBrowse1.Text);
            Facade.LoadRooms(tbBrowse2.Text);
            Facade.LoadPeriods(tbBrowse3.Text);

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.InitialDirectory = "c:\\";

            saveFileDialog1.Filter = "CSV Files (*.csv)|*.csv";

            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    Facade.save(saveFileDialog1.FileName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not write file to disk. Original error: " + ex.Message);
                }
            }

        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            Facade.setMutationRate(double.Parse(tbMutRate.Text));
            Facade.setPopulation(int.Parse(tbPopulation.Text));
            Facade.start();
            tbResults.Text = Facade.fitnesses();
        }

        // The program can be stopped when different events occur:
        private void btnStop1_Click(object sender, EventArgs e)
        {
            Facade.stop();
        }
    }
}